﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;

namespace eGrid_App
{
    class AnnotationMasterLayer : GraphicsLayer, ClonableLayer
    {

        public delegate void AnnotationAddedDelegate(Graphic g);
        public delegate void ClearGraphicsDelegate();
        public event AnnotationAddedDelegate AnnotationAdded;
        public event ClearGraphicsDelegate GraphicsCleared;

        public Layer CloneLayer()
        {
            AnnotationChildlayer cloneLayer = new AnnotationChildlayer();
            cloneLayer.ID = "AnnotationsLayer";
            cloneLayer.Opacity = Opacity;
            cloneLayer.Visible = Visible;
            foreach (Graphic g in Graphics)
            {
                Graphic newGraphic = new Graphic();
                newGraphic.Symbol = g.Symbol;
                newGraphic.Geometry = g.Geometry; 
                cloneLayer.Graphics.Add(newGraphic);
            }


            AnnotationAdded += cloneLayer.AddGraphic; // TODO unsubscribe us when child layer is destroyed
            GraphicsCleared += cloneLayer.Graphics.Clear;

            return cloneLayer;

        }

        public void AddGraphic(Graphic g)
        {
            Graphics.Add(g);
            AnnotationAdded(g);
        }

        public void ClearChildGraphics()
        {
            Graphics.Clear();
            GraphicsCleared();

        }

    }
}
